package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 测试套包 测试套包折扣率(%) 计算
 */
public class CalculateDiscountRateAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if(!KiteStringUtils.isEmpty(value)){
            if(KiteStringUtils.isEqual("200003735",attr.getFieldName()) ){
                KiteAttr amountAttr = inst.getAttr("200003736");
                String amount = amountAttr.getValue();
                if(!KiteStringUtils.isEmpty(amount)){
                    String discountRate = StrTools.numDivide(amount,value,2);
                    discountRate = StrTools.numMultiply(discountRate,"100");
                    if (discountRate.endsWith(".00")) {
                        discountRate = discountRate.substring(0, discountRate.length() - 3);
                    }
                    operateInstAttrService.setAttrValue(inst, "200003737", discountRate, data.getSceneInstId(), null);
                }
            }
            if(KiteStringUtils.isEqual("200003736",attr.getFieldName())){
                KiteAttr priceAttr = inst.getAttr("200003735");
                String   price =  priceAttr.getValue();
                if(!KiteStringUtils.isEmpty(price)){
                    String discountRate = StrTools.numDivide(value,price,2);
                    discountRate = StrTools.numMultiply(discountRate,"100");
                    if (discountRate.endsWith(".00")) {
                        discountRate = discountRate.substring(0, discountRate.length() - 3);
                    }
                    operateInstAttrService.setAttrValue(inst, "200003737", discountRate, data.getSceneInstId(), null);
                }
            }
        }

        super.set(attr, inst, value, data);
    }
}
